package com.matt.service.Photo.impl;

import com.matt.commons.exception.BusinessException;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.Feeds;
import com.matt.domain.photoalbum.Photo;
import com.matt.domain.photoalbum.PhotoAlbum;
import com.matt.foundation.model.ErrorCode;
import com.matt.repository.ClassesRepository;
import com.matt.repository.FeedsRepository;
import com.matt.repository.photoAlbum.PhotoAlbumRepository;
import com.matt.repository.photoAlbum.PhotoRepository;
import com.matt.service.Photo.PhotoService;
import com.matt.service.UploadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;


@Service
public class PhotoServiceImpl implements PhotoService {

    @Autowired
    PhotoAlbumRepository albumRepository;

    @Autowired
    PhotoRepository photoRepository;

    @Autowired
    ClassesRepository classRepository;

    @Autowired
    UploadService uploadService;

    @Autowired
    FeedsRepository feedsRepository;

    @Override
    public Page<Photo> list(String areaId, String albumId, PagerQuery pagerQuery) {

        PhotoAlbum album = albumRepository.getOneByAreaIdAndIdAndTypeAndDeletedIsFalse(areaId, albumId,1);

        if (album == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "相册不存在" );
        }
        PageRequest pageRequest = PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize(),
                Sort.by("created").descending());
        return photoRepository.findAllByDeletedIsFalseAndAlbumId(albumId, pageRequest);
    }

    @Override
    public List<Photo> listByAlbumId(String albumId) {
        if (StringUtils.isEmpty(albumId)){
            return Collections.emptyList();
        }
        return photoRepository.findAllByDeletedIsFalseAndAlbumId(albumId);
    }

    @Override
    @Transactional
    public List<Photo> addMultiple(String areaId, String albumId, String staffId, String title, String description, MultipartFile[] files){
        PhotoAlbum album = albumRepository.getOneByAreaIdAndIdAndTypeAndDeletedIsFalse(areaId, albumId,1);
//        if (album == null) {
//            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "相册不存在");
//            albumId = areaId;
//        }

        List<Photo> result = new ArrayList<>();
        List<Feeds> feedsList = new ArrayList<>();
        for (int i = 0; i < files.length; i++){
            String resURL = uploadService.uploadPhoto(staffId, (album==null)?areaId:albumId, files[i]);

            Photo photo = new Photo();
            photo.setAreaId(areaId);
            photo.setCreated(new Date());
            photo.setResURL(resURL);
            if (album != null) {
                photo.setAlbumId(albumId);
            }
            photo.setStaffId(staffId);
            photo.setTitle(title);
            photo.setDescription(description);

            result.add(photo);

            if (null != album.getChildrenId()){
                Feeds feeds = new Feeds();
                feeds.setChildrenId(album.getChildrenId());
                feeds.setPlace(areaId);
                feeds.setStaffId(staffId);
                feeds.setEventType("010");
                feeds.setTitle(title);
                feeds.setImages(resURL);
                feeds.setContent(description);
                feeds.setType(10);
                feeds.setTypeVal("相册");
                feedsList.add(feeds);
            }
        }
        photoRepository.saveAll(result);
//        feedsRepository.saveAll(feedsList);
        album.setPhotoCount(album.getPhotoCount() + result.size());
        albumRepository.save(album);

        return result;
    }

    @Override
    @Transactional
    public Photo add(String areaId, String albumId, String staffId, String title, String description, MultipartFile file) {

        PhotoAlbum album = albumRepository.getOneByAreaIdAndIdAndTypeAndDeletedIsFalse(areaId, albumId,1);
        if (album == null) {
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "相册不存在");
        }


        String resURL = uploadService.uploadPhoto(staffId, albumId, file);

        Photo photo = new Photo();
        photo.setAreaId(areaId);
        photo.setCreated(new Date());
        photo.setAlbumId(albumId);
        photo.setStaffId(staffId);
        photo.setResURL(resURL);
        photo.setTitle(title);
        photo.setDescription(description);
//        if (null != album.getChildrenId()){
//            Feeds feeds = new Feeds();
//            feeds.setChildrenId(album.getChildrenId());
//            feeds.setPlace(areaId);
//            feeds.setStaffId(staffId);
//            feeds.setEventType("010");
//            feeds.setTitle(title);
//            feeds.setImages(resURL);
//            feeds.setContent(description);
//            feeds.setType(10);
//            feeds.setTypeVal("相册");
//            feedsRepository.save(feeds);
//        }
        photoRepository.save(photo);

        return photo;
    }

    @Override
    public void delete(String areaId, String albumId, String staffId, String photoId) {
        Photo p = null;
        if (StringUtils.isNotEmpty(albumId)) {
            PhotoAlbum album = albumRepository.getOneByAreaIdAndIdAndTypeAndDeletedIsFalse(areaId, albumId,1);
            if (album == null) {
                throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "相册不存在");
            }

            //
            p = photoRepository.getOneByAlbumIdAndIdAndDeletedIsFalse(albumId, photoId);

        }else{
            p = photoRepository.getOneByStaffIdAndIdAndDeletedIsFalse(staffId, photoId);
        }
        //
        p.setDeleted(true);

        photoRepository.save(p);

        return;
    }

    @Override
    public void deleteAll(String areaId, String albumId, String staffId, String[] photoIds){

        if (StringUtils.isEmpty(albumId) || photoIds == null || photoIds.length == 0 || photoIds.length > 99) {
            throw new BusinessException(ErrorCode.PARAM_CAN_NOT_BLANK, "参数不能为空");
        }

        PhotoAlbum album = albumRepository.getOneByAreaIdAndIdAndTypeAndDeletedIsFalse(areaId, albumId,1);
        if (album == null) {
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "相册不存在");
        }

        List<Photo> photos = photoRepository.findAllByDeletedIsFalseAndAreaIdAndAlbumIdAndIdIn(areaId, albumId, photoIds);

        if (photos == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "照片不存在");
        }

        if (photos.size() != photoIds.length){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "你猜什么东西错了?");
        }

        for (Photo p : photos){
            p.setDeleted(true);
        }

        photoRepository.saveAll(photos);
    }


    @Override
    public Photo move(String areaId, String photoId, String albumId) {
        PhotoAlbum album = albumRepository.getOneByAreaIdAndIdAndTypeAndDeletedIsFalse(areaId, albumId,1);
        if (album == null) {
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "相册不存在");
        }

        Photo p = photoRepository.getOneByAreaIdAndIdAndDeletedIsFalse(areaId, photoId);

        if (p == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "照片不存在");
        }

        p.setAlbumId(albumId);

        photoRepository.save(p);

        return p;
    }


    public Photo update(String areaId, String photoId, String title, String description){

        Photo p = photoRepository.getOneByAreaIdAndIdAndDeletedIsFalse(areaId, photoId);

        if (p == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "照片不存在");
        }

        if (StringUtils.isNotEmpty(title)) {
            p.setTitle(title);
        }

        if(StringUtils.isNotEmpty(description)){
            p.setDescription(description);
        }

        photoRepository.save(p);

        return p;
    }
}
